<template>
  <el-row :gutter="40" class="panel-group">
    <el-col :xs="xs" :sm="sm" :md="md" :lg="lg" :xl="xl" class="card-panel-col">
      <div class="card-panel">
        <div class="card-panel-icon-wrapper icon-movie">
          <svg-icon icon-class="skill" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            剧本总数
          </div>
          <count-to :start-val="0" :end-val="movieCount" :duration="3600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="xs" :sm="sm" :md="md" :lg="lg" :xl="xl" class="card-panel-col">
      <div class="card-panel">
        <div class="card-panel-icon-wrapper icon-household">
          <svg-icon icon-class="star" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            店铺总数
          </div>
          <count-to :start-val="0" :end-val="shopCount" :duration="3600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="xs" :sm="sm" :md="md" :lg="lg" :xl="xl" class="card-panel-col">
      <div class="card-panel">
        <div class="card-panel-icon-wrapper icon-people">
          <svg-icon icon-class="peoples" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            用户总数
          </div>
          <count-to :start-val="0" :end-val="userTotal" :duration="2600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="xs" :sm="sm" :md="md" :lg="lg" :xl="xl" class="card-panel-col">
      <div class="card-panel">
        <div class="card-panel-icon-wrapper icon-device">
          <svg-icon icon-class="dashboard" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            设备总数
          </div>
          <count-to :start-val="0" :end-val="deviceTotal" :duration="2600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="xs" :sm="sm" :md="md" :lg="lg" :xl="xl" class="card-panel-col">
      <div class="card-panel">
        <div class="card-panel-icon-wrapper icon-franchisee">
          <svg-icon icon-class="example" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            加盟商总数
          </div>
          <count-to :start-val="0" :end-val="franchiseeCount" :duration="3200" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="xs" :sm="sm" :md="md" :lg="lg" :xl="xl" class="card-panel-col">
      <div class="card-panel">
        <div class="card-panel-icon-wrapper icon-room">
          <svg-icon icon-class="table" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            房间总数
          </div>
          <count-to :start-val="0" :end-val="roomCount" :duration="3600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import CountTo from 'vue-count-to'
import { getUserTotalCount, getDeviceTotalCount, getOrgUnitCount, getRoomCount, getMovieCount } from '@/api/dashboard'

export default {
  components: {
    CountTo
  },
  data() {
    return {
      xs: 12,
      sm: 8,
      md: 6,
      lg: 4,
      xl: 4,
      userTotal: 0,
      deviceTotal: 0,
      franchiseeCount: 0,
      shopCount: 0,
      roomCount: 0,
      movieCount: 0,
      timer: null,
      totalTimer: null
    }
  },
  mounted() {
    this.timer = setInterval(this.timeoutRunner, 10 * 1000) // 在线用户和在线设备10秒刷新一次
    this.totalTimer = setInterval(this.totalTimeoutRunner, 1000 * 60 * 3) // 总数的统计,3分钟刷新一次,不需要太频繁
    this.timeoutRunner()
    this.totalTimeoutRunner()
  },
  destroyed() {
    if (this.timer) {
      clearInterval(this.timer)
      this.timer = null
    }
    if (this.totalTimer) {
      clearInterval(this.totalTimer)
      this.totalTimer = null
    }
  },
  methods: {
    timeoutRunner() {
    },
    totalTimeoutRunner() {
      this.loadUserTotal()
      this.loadDeviceTotal()
      this.loadFranchiseeCount()
      this.loadShopCount()
      this.loadRoomCount()
      this.loadMovieCount()
    },
    loadUserTotal() {
      getUserTotalCount().then(({ success, message, data }) => {
        const count = parseInt(data)
        if (count >= 0) {
          this.userTotal = count
        } else if (!success) {
          this.$message({
            type: 'error',
            message: message || '获取用户总数失败！'
          })
        }
      })
    },
    loadDeviceTotal() {
      getDeviceTotalCount().then(({ success, message, data }) => {
        const count = parseInt(data)
        if (count >= 0) {
          this.deviceTotal = count
        } else if (!success) {
          this.$message({
            type: 'error',
            message: message || '获取设备总数失败！'
          })
        }
      })
    },
    loadFranchiseeCount() {
      getOrgUnitCount({ type: 2 }).then(({ success, message, data }) => {
        const count = parseInt(data)
        if (count >= 0) {
          this.franchiseeCount = count
        } else if (!success) {
          this.$message({
            type: 'error',
            message: message || '获取加盟商总数失败！'
          })
        }
      })
    },
    loadShopCount() {
      getOrgUnitCount({ type: 1 }).then(({ success, message, data }) => {
        const count = parseInt(data)
        if (count >= 0) {
          this.shopCount = count
        } else if (!success) {
          this.$message({
            type: 'error',
            message: message || '获取店铺总数失败！'
          })
        }
      })
    },
    loadRoomCount() {
      getRoomCount().then(({ success, message, data }) => {
        const count = parseInt(data)
        if (count >= 0) {
          this.roomCount = count
        } else if (!success) {
          this.$message({
            type: 'error',
            message: message || '获取房间总数失败！'
          })
        }
      })
    },
    loadMovieCount() {
      getMovieCount().then(({ success, message, data }) => {
        const count = parseInt(data)
        if (count >= 0) {
          this.movieCount = count
        } else if (!success) {
          this.$message({
            type: 'error',
            message: message || '获取剧本总数失败！'
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.panel-group {
  margin-top: 18px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-people {
        background: #40c9c6;
      }

      .icon-device {
        background: #36a3f7;
      }

      .icon-franchisee {
        background: #f4516c;
      }

      .icon-household {
        background: #34bfa3
      }

      .icon-room {
        background: #97a8be;
      }

      .icon-movie {
        background: #66967e;
      }
    }

    .icon-people {
      color: #40c9c6;
    }

    .icon-device {
      color: #36a3f7;
    }

    .icon-franchisee {
      color: #f4516c;
    }

    .icon-household {
      color: #34bfa3
    }

    .icon-room {
      color: #97a8be;
    }

    .icon-movie {
      color: #66967e;
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      font-size: 20px;
      margin: 26px 26px 26px 0;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width:550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
</style>
